package top.fzqblog.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Random;

import top.fzqblog.firstapp.R;

/**
 * Created by 抽离 on 2017/8/25 0025.
 */

public class RecycleViewAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private List<String> contents;

    private LayoutInflater inflater;

    private List<Integer> images;

    public RecycleViewAdapter(Context context, List<String> contents, List<Integer> images) {
        this.contents = contents;
        this.images = images;
        this.inflater = LayoutInflater.from(context);
    }

    //创建新View，被LayoutManager所调用
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycleviewtextview, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(contents.get(position));
//        ViewGroup.LayoutParams lp = holder.imageView.getLayoutParams();
        holder.imageView.setBackgroundResource(images.get(position));
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return contents.size();
    }


    public void addItem(int position){
        images.add(position, R.mipmap.bg1);
        notifyItemInserted(position);
    }

    public void remove(int position){
        images.remove(position);
        notifyItemRemoved(position);
    }
}



//自定义的ViewHolder，持有每个Item的的所有界面元素
class MyViewHolder extends RecyclerView.ViewHolder{

    public TextView textView;

    public ImageView imageView;

    public MyViewHolder(View itemView) {
        super(itemView);
        textView =  itemView.findViewById(R.id.recycleviewtv);
        imageView = itemView.findViewById(R.id.recycleviewiv);
    }
}